package choreganizer;

import java.io.*;
import java.util.*;
import java.text.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.util.regex.*;
import java.util.Calendar;
import java.util.GregorianCalendar;




class ConfigSched_EditChore
extends JPanel
{
	private JList lst_val;
	private JButton btn_next;
	private ScheduleData tempSched;
	private String noChoreStr = "- No Chores Exist -";
	
	public ConfigSched_EditChore(ChoreData[] chores, ScheduleData tempSched, JButton btn_next, String method)
	{
		this.tempSched = tempSched;
		this.btn_next = btn_next;
		
		//System.out.println(tempSched.toXMLString());

		
		// Create components
		JLabel lbl_desc = new JLabel("Assign a chore to this schedules");
		JSeparator sep  = new JSeparator();
		lst_val         = new JList();
		JScrollPane scl_lst_val = new JScrollPane(lst_val);
		
		lst_val.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
		lst_val.addListSelectionListener( new ChoreSelectListener() );
		
		
		if (chores.length > 0)
		{
			lst_val.setListData( chores );
		}
		else
		{
			lst_val.setListData( new String[] {noChoreStr} );
			lst_val.setEnabled( false );
		}
		
		if (tempSched.getChoreID() != null)
		{
			for (int i=0; i < chores.length; i++)
			{
				if ( chores[i].getID().equals(tempSched.getChoreID()) )
				{
					lst_val.setSelectedIndex(i);
				}
			}
		}

		if (method.equals("edit"))
		{
			lbl_desc.setText("Change the chore assigned to this schedule");
		}
		else if (method.equals("del"))
		{
			lbl_desc.setText("View the chore assigned to this schedule");
			lst_val.setEnabled(false);
			btn_next.setEnabled(true);
		}
		
		
		// Set layout
		GridBagConstraints gbc = new GridBagConstraints();;
		GridBagLayout gbl = new GridBagLayout();
		setLayout( gbl );
		
		gbc = new GridBagConstraints(0,0,1,1,0,0,GridBagConstraints.PAGE_START,GridBagConstraints.HORIZONTAL,new Insets(20,6,2,6),8,8);
		gbl.setConstraints(lbl_desc, gbc);
		gbc = new GridBagConstraints(0,1,1,1,1,0,GridBagConstraints.PAGE_START,GridBagConstraints.HORIZONTAL,new Insets(2,6,6,6),8,8);
		gbl.setConstraints(sep, gbc);
		gbc = new GridBagConstraints(0,2,1,1,0,1,GridBagConstraints.PAGE_START,GridBagConstraints.BOTH,new Insets(2,30,2,30),8,8);
		gbl.setConstraints(scl_lst_val, gbc);
		
		add(lbl_desc);
		add(sep);
		add(scl_lst_val);
	}
	
	class ChoreSelectListener
	implements ListSelectionListener
	{
		public ChoreSelectListener()
		{
		}
		
		public void valueChanged(ListSelectionEvent e) 
		{
			if (e.getValueIsAdjusting() == false) 
			{
				int index = lst_val.getSelectedIndex();
				if (index >= 0)
					btn_next.setEnabled(true);
				
				tempSched.setChoreID( ((ChoreData)lst_val.getSelectedValue()).getID() );
			}
		}
	}
	
}



class ConfigSched_EditYear
extends JPanel
{
	private JList lst_val;
	private JComboBox cmb_type;
	private ScheduleData tempSched;
	private JButton btn_next;
	
	
	public ConfigSched_EditYear(ScheduleData tempSched, JButton btn_next, String method)
	{
		this.tempSched = tempSched;
		this.btn_next = btn_next;
		
		//System.out.println(tempSched.toXMLString());
		
		// Create components
		JLabel lbl_desc = new JLabel("Set the scheduling for the year");
		JLabel lbl_type = new JLabel("Type:",JLabel.RIGHT);
		JLabel lbl_val  = new JLabel("Values:",JLabel.RIGHT);
		JSeparator sep  = new JSeparator();
		cmb_type        = new JComboBox();
		lst_val         = new JList();
		JScrollPane scl_lst_val = new JScrollPane(lst_val);
		
		
		
		
		// Populate the combo box w/ the YearTypes
		ScheduleTypes.YearTypes[] types = ScheduleTypes.AllYearTypes();
		for (int i=0; i < types.length; i++)
			cmb_type.insertItemAt(types[i].toString(),i);
		
		// Attach listeners
		cmb_type.addActionListener(new YearTypeListener());
		lst_val.addListSelectionListener(new YearValueListener());
		
		// If the ScheduleData item's YearType is set, make that our selected item in the combo box
		// This should trigger the list's listener which will populate it
		if (tempSched.getYearType() != null)
			cmb_type.setSelectedItem( tempSched.getYearType().toString() );
		else
			cmb_type.setSelectedIndex(0);
		
		
		
		// If the ScheduleData item's year values are set, make them selected in our list
		if (tempSched.getScheduledYear() != null)
		{
			String[] values = tempSched.getScheduledYear();
			java.util.List<Integer> select = new java.util.Vector<Integer>();
			
			ListModel lm = lst_val.getModel();
			for (int i=0; i < lm.getSize(); i++)
			{
				String item = (String)lm.getElementAt(i);
				
				// See if this item is in the ScheduleData's array
				for(String k: values)
					if (k.equals(item))
						select.add(i);
			}
			
			// Put the selected items into an array, and pass it to the list
			int[] selectedArray = new int[select.size()];
			for (int i=0; i < select.size(); i++)
				selectedArray[i] = select.get(i);
			
			lst_val.setSelectedIndices( selectedArray );
			
			if (lst_val.getSelectedIndex() >= 0)
				btn_next.setEnabled(true);
		}
		
		
		if (method.equals("add"))
		{
			lbl_desc.setText("Add the scheduling for the year");
		}
		else if (method.equals("del"))
		{
			lbl_desc.setText("View the scheduling for the year");
			cmb_type.setEnabled(false);
			lst_val.setEnabled(false);
			btn_next.setEnabled(true);
		}
		
		
		
		// Set layout
		GridBagConstraints gbc = new GridBagConstraints();;
		GridBagLayout gbl = new GridBagLayout();
		setLayout(gbl);
		gbc = new GridBagConstraints(0,0,4,1,1,0,GridBagConstraints.NORTH,GridBagConstraints.HORIZONTAL,new Insets(20,6,2,6),8,8);
		gbl.setConstraints(lbl_desc, gbc);
		gbc = new GridBagConstraints(0,1,4,1,1,0,GridBagConstraints.NORTH,GridBagConstraints.HORIZONTAL,new Insets(2,6,2,6),8,8);
		gbl.setConstraints(sep, gbc);
		gbc = new GridBagConstraints(0,2,1,1,0,1,GridBagConstraints.NORTH,GridBagConstraints.NONE,new Insets(2,6,2,6),8,8);
		gbl.setConstraints(lbl_type, gbc);
		gbc = new GridBagConstraints(1,2,1,1,0,1,GridBagConstraints.NORTH,GridBagConstraints.HORIZONTAL,new Insets(2,6,2,6),8,8);
		gbl.setConstraints(cmb_type, gbc);
		gbc = new GridBagConstraints(2,2,1,1,0,1,GridBagConstraints.NORTH,GridBagConstraints.HORIZONTAL,new Insets(2,6,2,6),8,8);
		gbl.setConstraints(lbl_val, gbc);
		gbc = new GridBagConstraints(3,2,1,1,0,1,GridBagConstraints.NORTH,GridBagConstraints.HORIZONTAL,new Insets(2,6,2,6),8,8);
		gbl.setConstraints(scl_lst_val, gbc);
		
		// Add components to the panel
		add(lbl_desc);
		add(lbl_type);
		add(lbl_val);
		add(sep);
		add(cmb_type);
		add(scl_lst_val);
	}
	
	
	class YearTypeListener
	implements ActionListener
	{
		/*private String months[] = {
				"January", "February", "March", "April", "May", 
				"June", "July", "August", "September", "October", 
				"November", "December"};
				
		private String days[] = {
				"1st","2nd","3rd","4th","5th","6th","7th","8th","9th","10th",
				"11th","12th","13th","14th","15th","16th","17th","18th","19th","20th",
				"21st","22nd","23rd","24th","25th","26th","27th","28th","29th","30th","31st"};*/
				
		private String[] annual;
		private String[] specific;
				
		public YearTypeListener()
		{
			Calendar calendar = new GregorianCalendar();
			int year = calendar.get(Calendar.YEAR);
			
			annual = new String[20];
			for (int i=0; i < 20; i++)
				annual[i] = Integer.toString(i+1);
				
			specific = new String[20];
			for (int i=0; i < 20; i++)
				specific[i] = Integer.toString(year+i);
		}
		
		public void actionPerformed(ActionEvent e)
		{
			lst_val.clearSelection();
			lst_val.removeAll();
			
			ScheduleTypes.YearTypes yt = ScheduleTypes.YearToType( (String)cmb_type.getSelectedItem() );
			tempSched.setYearType( yt );
			
			if (yt == ScheduleTypes.YearTypes.annual)
			{
				lst_val.setListData(annual);
			}
			else if (yt == ScheduleTypes.YearTypes.specific)
			{
				lst_val.setListData(specific);
			}
		}
	}
	
	
	class YearValueListener
	implements ListSelectionListener
	{
		public YearValueListener()
		{
		}
		
		public void valueChanged(ListSelectionEvent e) 
		{
			if (e.getValueIsAdjusting() == false) 
			{
				ListModel lm = lst_val.getModel();
				int[] indicies = lst_val.getSelectedIndices();
				
				// Get a list of all the String[]'s that are selected
				String[] years = new String[indicies.length];
				for (int i=0; i < indicies.length; i++)
				{
					years[i] = (String)lm.getElementAt(indicies[i]);
				}
				
				tempSched.setScheduledYear( years );
			}
			
			if (lst_val.getSelectedIndex() >= 0)
				btn_next.setEnabled(true);
			else
				btn_next.setEnabled(false);
		}
	}
}




class ConfigSched_EditMonth
extends JPanel
{
	private JList lst_val;
	private JComboBox cmb_type;
	private ScheduleData tempSched;
	private JButton btn_next;
	
	
	public ConfigSched_EditMonth(ScheduleData tempSched, JButton btn_next, String method)
	{
		this.tempSched = tempSched;
		this.btn_next = btn_next;
		
		//System.out.println(tempSched.toXMLString());
		
		// Create components
		JLabel lbl_desc = new JLabel("Set the scheduling for the month");
		JLabel lbl_type = new JLabel("Type:",JLabel.RIGHT);
		JLabel lbl_val  = new JLabel("Values:",JLabel.RIGHT);
		JSeparator sep  = new JSeparator();
		cmb_type        = new JComboBox();
		lst_val         = new JList();
		JScrollPane scl_lst_val = new JScrollPane(lst_val);
		
		
		
		// Populate the combo box w/ the MonthTypes
		ScheduleTypes.MonthTypes[] types = ScheduleTypes.AllMonthTypes();
		for (int i=0; i < types.length; i++)
			cmb_type.insertItemAt(types[i].toString(),i);
		
		// Attach listeners
		cmb_type.addActionListener(new MonthTypeListener());
		lst_val.addListSelectionListener(new MonthValueListener());
		
		// If the ScheduleData item's MonthType is set, make that our selected item in the combo box
		// This should trigger the list's listener which will populate it
		if (tempSched.getMonthType() != null)
			cmb_type.setSelectedItem( tempSched.getMonthType().toString() );
		else
			cmb_type.setSelectedIndex(0);
		
		
		
		// Select any current month values in our list
		String[] values = tempSched.getScheduledMonth();
		if (values != null && values.length > 0)
		{
			ScheduleTypes.MonthTypes mt = ScheduleTypes.MonthToType( (String)cmb_type.getSelectedItem() );
			tempSched.setMonthType( mt );
			java.util.List<Integer> select = new java.util.Vector<Integer>();
			
			if (mt == ScheduleTypes.MonthTypes.annual)
			{
				ListModel lm = lst_val.getModel();
				for (int i=0; i < lm.getSize(); i++)
				{
					
					for (String k: values)
					{
						int val = Integer.decode(k);
						if (val == i+1)
							select.add(i);
					}
					//String item = (String)lm.getElementAt(i);
				}
			}
			else if (mt == ScheduleTypes.MonthTypes.specific)
			{
				ListModel lm = lst_val.getModel();
				for (int i=0; i < lm.getSize(); i++)
				{
					
					for (String k: values)
					{
						int val = Integer.decode(k);
						if (val == i)
							select.add(i);
					}
					//String item = (String)lm.getElementAt(i);
				}
			}
			else if (mt == ScheduleTypes.MonthTypes.unspecified)
			{
				select.add(0);
			}
			
			// Put the selected items into an array, and pass it to the list
			int[] selectedArray = new int[select.size()];
			for (int i=0; i < select.size(); i++)
				selectedArray[i] = select.get(i);
			
			lst_val.setSelectedIndices( selectedArray );
			
			if (lst_val.getSelectedIndex() >= 0)
				btn_next.setEnabled(true);
		}

		
		
		if (method.equals("add"))
		{
			lbl_desc.setText("Add the scheduling for the month");
		}
		else if (method.equals("del"))
		{
			lbl_desc.setText("View the scheduling for the month");
			cmb_type.setEnabled(false);
			lst_val.setEnabled(false);
			btn_next.setEnabled(true);
		}
		
		
		// Set layout
		GridBagConstraints gbc = new GridBagConstraints();;
		GridBagLayout gbl = new GridBagLayout();
		setLayout(gbl);
		gbc = new GridBagConstraints(0,0,4,1,1,0,GridBagConstraints.NORTH,GridBagConstraints.HORIZONTAL,new Insets(20,6,2,6),8,8);
		gbl.setConstraints(lbl_desc, gbc);
		gbc = new GridBagConstraints(0,1,4,1,1,0,GridBagConstraints.NORTH,GridBagConstraints.HORIZONTAL,new Insets(2,6,2,6),8,8);
		gbl.setConstraints(sep, gbc);
		gbc = new GridBagConstraints(0,2,1,1,0,1,GridBagConstraints.NORTH,GridBagConstraints.NONE,new Insets(2,6,2,6),8,8);
		gbl.setConstraints(lbl_type, gbc);
		gbc = new GridBagConstraints(1,2,1,1,0,1,GridBagConstraints.NORTH,GridBagConstraints.HORIZONTAL,new Insets(2,6,2,6),8,8);
		gbl.setConstraints(cmb_type, gbc);
		gbc = new GridBagConstraints(2,2,1,1,0,1,GridBagConstraints.NORTH,GridBagConstraints.HORIZONTAL,new Insets(2,6,2,6),8,8);
		gbl.setConstraints(lbl_val, gbc);
		gbc = new GridBagConstraints(3,2,1,1,0,1,GridBagConstraints.NORTH,GridBagConstraints.HORIZONTAL,new Insets(2,6,2,6),8,8);
		gbl.setConstraints(scl_lst_val, gbc);
		
		// Add components to the panel
		add(lbl_desc);
		add(lbl_type);
		add(lbl_val);
		add(sep);
		add(cmb_type);
		add(scl_lst_val);
	}
	
	
	class MonthTypeListener
	implements ActionListener
	{
		private String annual[] = {
				"1","2","3","4","5","6","7","8","9","10","11","12","13",
				"14","15","16","17","18","19","20","21","22","23","24"};
		private String specific[] = {
				"January", "February", "March", "April", "May", 
				"June", "July", "August", "September", "October", 
				"November", "December"};
		private String[] unspecified = {"- UNUSED -"};		
		

				
		public MonthTypeListener()
		{
		}
		
		public void actionPerformed(ActionEvent e)
		{
			lst_val.clearSelection();
			lst_val.removeAll();
			
			ScheduleTypes.MonthTypes mt = ScheduleTypes.MonthToType( (String)cmb_type.getSelectedItem() );
			tempSched.setMonthType( mt );
			
			if (mt == ScheduleTypes.MonthTypes.annual)
			{
				lst_val.setEnabled(true);
				lst_val.setListData(annual);
			}
			else if (mt == ScheduleTypes.MonthTypes.specific)
			{
				lst_val.setEnabled(true);
				lst_val.setListData(specific);
			}
			else if (mt == ScheduleTypes.MonthTypes.unspecified)
			{
				lst_val.setEnabled(false);
				lst_val.setListData(unspecified);
				lst_val.setSelectedIndex(0);
				btn_next.setEnabled(true);
			}
		}
	}
	
	
	class MonthValueListener
	implements ListSelectionListener
	{
		public MonthValueListener()
		{
		}
		
		public void valueChanged(ListSelectionEvent e) 
		{
			if (e.getValueIsAdjusting() == false)
			{
				ListModel lm = lst_val.getModel();
				int[] indicies = lst_val.getSelectedIndices();
				
				// Set the tempSched's montType based on selection
				ScheduleTypes.MonthTypes mt = ScheduleTypes.MonthToType( (String)cmb_type.getSelectedItem() );
				
				if (mt == ScheduleTypes.MonthTypes.annual)
				{
					String[] months = new String[indicies.length];
					for (int i=0; i < indicies.length; i++)
					{
						months[i] = Integer.toString(indicies[i]+1);
					}
					
					tempSched.setScheduledMonth( months );	
				}
				else if (mt == ScheduleTypes.MonthTypes.specific)
				{
					String[] months = new String[indicies.length];
					for (int i=0; i < indicies.length; i++)
					{
						months[i] = Integer.toString(indicies[i]);
					}
					
					tempSched.setScheduledMonth( months );	
				}
				else if (mt == ScheduleTypes.MonthTypes.unspecified)
				{
					String[] empty = {};
					lst_val.setSelectedIndex(0);
					tempSched.setScheduledMonth( empty );
				}
			}
			
			// Determine if the next button is enabled
			if (lst_val.getSelectedIndex() >= 0)
			{
				btn_next.setEnabled(true);
			}
			else
			{
				btn_next.setEnabled(false);
			}
			
		}
	}
}




class ConfigSched_EditDay
extends JPanel
{
	private JList lst_val;
	private JComboBox cmb_type;
	private ScheduleData tempSched;
	private JButton btn_next;
	
	
	public ConfigSched_EditDay(ScheduleData tempSched, JButton btn_next, String method)
	{
		this.tempSched = tempSched;
		this.btn_next = btn_next;
		
		//System.out.println(tempSched.toXMLString());
		
		// Create components
		JLabel lbl_desc = new JLabel("Set the scheduling for the day");
		JLabel lbl_type = new JLabel("Type:",JLabel.RIGHT);
		JLabel lbl_val  = new JLabel("Values:",JLabel.RIGHT);
		JSeparator sep  = new JSeparator();
		cmb_type        = new JComboBox();
		lst_val         = new JList();
		JScrollPane scl_lst_val = new JScrollPane(lst_val);
		
		
		// Populate the combo box w/ the DayTypes
		if (tempSched.getMonthType() == ScheduleTypes.MonthTypes.unspecified)
		{
			cmb_type.insertItemAt(ScheduleTypes.DayTypes.annual.toString(),0);
		}
		else
		{
			cmb_type.insertItemAt(ScheduleTypes.DayTypes.annual.toString(),0);
			cmb_type.insertItemAt(ScheduleTypes.DayTypes.specific.toString(),1);
			cmb_type.insertItemAt(ScheduleTypes.DayTypes.calendardays.toString(),2);
		}
		
		
		// Attach listeners
		cmb_type.addActionListener(new DayTypeListener());
		lst_val.addListSelectionListener(new DayValueListener());
		
		// If the ScheduleData item's DayType is set, make that our selected item in the combo box
		// This should trigger the list's listener which will populate it
		if (tempSched.getDayType() != null)
			if (tempSched.getMonthType() != ScheduleTypes.MonthTypes.unspecified)
				cmb_type.setSelectedItem( tempSched.getDayType().toString() );
		if (cmb_type.getSelectedIndex() < 0)
			cmb_type.setSelectedIndex(0);

		
		
		
		// Select any current day values in our list
		String[] values = tempSched.getScheduledDay();
		if (values != null && values.length > 0)
		{
			ScheduleTypes.DayTypes dt = ScheduleTypes.DayToType( (String)cmb_type.getSelectedItem() );
			tempSched.setDayType( dt );
			java.util.List<Integer> select = new java.util.Vector<Integer>();
			
			ListModel lm = lst_val.getModel();
			for (int i=0; i < lm.getSize(); i++)
			{
				for (String k: values)
				{
					int val = Integer.decode(k);
					if (val == i+1)
						select.add(i);
				}
			}
			
			int[] selectedArray = new int[select.size()];
			for (int i=0; i < select.size(); i++)
				selectedArray[i] = select.get(i);
			
			lst_val.setSelectedIndices( selectedArray );
			
			if (lst_val.getSelectedIndex() >= 0)
				btn_next.setEnabled(true);
		}
		
		
		
		if (method.equals("add"))
		{
			lbl_desc.setText("Add the scheduling for the day");
		}
		else if (method.equals("del"))
		{
			lbl_desc.setText("View the scheduling for the day");
			cmb_type.setEnabled(false);
			lst_val.setEnabled(false);
			btn_next.setEnabled(true);
		}

		
		
		// Set layout
		GridBagConstraints gbc = new GridBagConstraints();;
		GridBagLayout gbl = new GridBagLayout();
		setLayout(gbl);
		gbc = new GridBagConstraints(0,0,4,1,1,0,GridBagConstraints.NORTH,GridBagConstraints.HORIZONTAL,new Insets(20,6,2,6),8,8);
		gbl.setConstraints(lbl_desc, gbc);
		gbc = new GridBagConstraints(0,1,4,1,1,0,GridBagConstraints.NORTH,GridBagConstraints.HORIZONTAL,new Insets(2,6,2,6),8,8);
		gbl.setConstraints(sep, gbc);
		gbc = new GridBagConstraints(0,2,1,1,0,1,GridBagConstraints.NORTH,GridBagConstraints.NONE,new Insets(2,6,2,6),8,8);
		gbl.setConstraints(lbl_type, gbc);
		gbc = new GridBagConstraints(1,2,1,1,0,1,GridBagConstraints.NORTH,GridBagConstraints.HORIZONTAL,new Insets(2,6,2,6),8,8);
		gbl.setConstraints(cmb_type, gbc);
		gbc = new GridBagConstraints(2,2,1,1,0,1,GridBagConstraints.NORTH,GridBagConstraints.HORIZONTAL,new Insets(2,6,2,6),8,8);
		gbl.setConstraints(lbl_val, gbc);
		gbc = new GridBagConstraints(3,2,1,1,1,1,GridBagConstraints.NORTH,GridBagConstraints.HORIZONTAL,new Insets(2,6,2,6),8,8);
		gbl.setConstraints(scl_lst_val, gbc);
		
		// Add components to the panel
		add(lbl_desc);
		add(lbl_type);
		add(lbl_val);
		add(sep);
		add(cmb_type);
		add(scl_lst_val);
	}
	
	
	class DayTypeListener
	implements ActionListener
	{
		private String annual[];
		private String specific[] = {
				"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
		private String calendardays[] = {
				"1st","2nd","3rd","4th","5th","6th","7th","8th","9th","10th",
				"11th","12th","13th","14th","15th","16th","17th","18th","19th","20th",
				"21st","22nd","23rd","24th","25th","26th","27th","28th","29th","30th","31st"};;		
		

				
		public DayTypeListener()
		{
			int max = 62;
			if (tempSched.getMonthType() != ScheduleTypes.MonthTypes.unspecified)
				max = 31;
			
			annual = new String[max];
			for (int i=0; i < max; i++)
			{
				annual[i] = "Every " + Integer.toString(i+1) + " day(s)";
			}
		}
		
		public void actionPerformed(ActionEvent e)
		{
			lst_val.clearSelection();
			lst_val.removeAll();
			
			ScheduleTypes.DayTypes dt = ScheduleTypes.DayToType( (String)cmb_type.getSelectedItem() );
			tempSched.setDayType( dt );
			
			if (dt == ScheduleTypes.DayTypes.annual)
			{
				lst_val.setListData(annual);
			}
			else if (dt == ScheduleTypes.DayTypes.specific)
			{
				lst_val.setListData(specific);
			}
			else if (dt == ScheduleTypes.DayTypes.calendardays)
			{
				lst_val.setListData(calendardays);
			}
		}
	}
	
	
	class DayValueListener
	implements ListSelectionListener
	{
		public DayValueListener()
		{
		}
		
		public void valueChanged(ListSelectionEvent e) 
		{
			if (e.getValueIsAdjusting() == false)
			{
				ListModel lm = lst_val.getModel();
				int[] indicies = lst_val.getSelectedIndices();
				
				String[] days = new String[indicies.length];
				for (int i=0; i < indicies.length; i++)
				{
					days[i] = Integer.toString(indicies[i]+1);
				}
				
				tempSched.setScheduledDay( days );
			}
			
			if (lst_val.getSelectedIndex() >= 0)
			{
				btn_next.setEnabled(true);
			}
			else
			{
				btn_next.setEnabled(false);
			}
			
		}
	}
}




class ConfigSched_EditTime
extends JPanel
{
	private JSpinner spn_val;
	private JComboBox cmb_type;
	private ScheduleData tempSched;
	private JButton btn_next;
	
	
	public ConfigSched_EditTime(ScheduleData tempSched, JButton btn_next, String method)
	{
		this.tempSched = tempSched;
		this.btn_next = btn_next;
		
		//System.out.println(tempSched.toXMLString());
		
		// Create components
		JLabel lbl_desc = new JLabel("Set the scheduling for the time");
		JLabel lbl_type = new JLabel("Type:",JLabel.RIGHT);
		JLabel lbl_val  = new JLabel("Values:",JLabel.RIGHT);
		JSeparator sep  = new JSeparator();
		cmb_type        = new JComboBox();
		spn_val         = new JSpinner();
		
		
		
		SpinnerDateModel spn_mdl = new SpinnerDateModel();
		spn_mdl.setCalendarField(Calendar.HOUR_OF_DAY);
		spn_val.setModel(spn_mdl);
		JSpinner.DateEditor spn_edt = new JSpinner.DateEditor(spn_val,"HH:mm");
		spn_val.setEditor(spn_edt);
		
		
		// Populate the combo box w/ the TimeTypes
		ScheduleTypes.TimeTypes[] types = ScheduleTypes.AllTimeTypes();
		for (int i=0; i < types.length; i++)
			cmb_type.insertItemAt(types[i].toString(),i);

		
		
		// Attach listeners
		cmb_type.addActionListener(new TimeTypeListener());
		spn_val.addChangeListener(new TimeValueListener());
		
		
		// If the ScheduleData item's TimeType is set, make that our selected item in the combo box
		// This should trigger the list's listener which will populate it
		if (tempSched.getTimeType() != null)
			cmb_type.setSelectedItem( tempSched.getTimeType().toString() );
		else
			cmb_type.setSelectedIndex(0);
		
		
		// Set the JSpinners value
		String[] values = tempSched.getScheduledTime();
		if (values != null && values.length > 0)
		{
			// We're only going to allow the user to input one time
			String val = values[0];
			
			// This will set the date for the spinners model.  Only the hour:minutes matter,
			// but we pass in an entiere date
			try {
				Date valueDate = new SimpleDateFormat("HH:mm").parse( val );
				spn_val.setValue(valueDate);
				
			} catch (Exception ex) {
				System.out.println(ex.getMessage());
			}
		}
		else
		{
			try {
				Date rightNow = new Date();
				spn_val.setValue(rightNow);
			} catch (Exception ex) {
				System.out.println(ex.getMessage());
			}
		}
		
		
		if (method.equals("add"))
		{
			lbl_desc.setText("Add the scheduling for the time");
		}
		else if (method.equals("del"))
		{
			lbl_desc.setText("View the scheduling for the time");
			cmb_type.setEnabled(false);
			spn_val.setEnabled(false);
			btn_next.setEnabled(true);
		}
		
		
		// Set layout
		GridBagConstraints gbc = new GridBagConstraints();;
		GridBagLayout gbl = new GridBagLayout();
		setLayout(gbl);
		gbc = new GridBagConstraints(0,0,4,1,1,0,GridBagConstraints.NORTH,GridBagConstraints.HORIZONTAL,new Insets(20,6,2,6),8,8);
		gbl.setConstraints(lbl_desc, gbc);
		gbc = new GridBagConstraints(0,1,4,1,1,0,GridBagConstraints.NORTH,GridBagConstraints.HORIZONTAL,new Insets(2,6,2,6),8,8);
		gbl.setConstraints(sep, gbc);
		gbc = new GridBagConstraints(0,2,1,1,0,1,GridBagConstraints.NORTH,GridBagConstraints.NONE,new Insets(2,6,2,6),8,8);
		gbl.setConstraints(lbl_type, gbc);
		gbc = new GridBagConstraints(1,2,1,1,0,1,GridBagConstraints.NORTH,GridBagConstraints.HORIZONTAL,new Insets(2,6,2,6),8,8);
		gbl.setConstraints(cmb_type, gbc);
		gbc = new GridBagConstraints(2,2,1,1,0,1,GridBagConstraints.NORTH,GridBagConstraints.HORIZONTAL,new Insets(2,6,2,6),8,8);
		gbl.setConstraints(lbl_val, gbc);
		gbc = new GridBagConstraints(3,2,1,1,1,1,GridBagConstraints.NORTH,GridBagConstraints.HORIZONTAL,new Insets(2,6,2,6),8,8);
		gbl.setConstraints(spn_val, gbc);
		
		// Add components to the panel
		add(lbl_desc);
		add(lbl_type);
		add(lbl_val);
		add(sep);
		add(cmb_type);
		add(spn_val);
	}
	
	
	class TimeTypeListener
	implements ActionListener
	{
		public TimeTypeListener()
		{
		}
		
		public void actionPerformed(ActionEvent e)
		{
			ScheduleTypes.TimeTypes tt = ScheduleTypes.TimeToType( (String)cmb_type.getSelectedItem() );
			tempSched.setTimeType( tt );
		}
	}
	
	
	class TimeValueListener
	implements ChangeListener
	{
		public TimeValueListener()
		{
		}
		
		public void stateChanged(ChangeEvent e)
		{
			Date time = (Date)spn_val.getValue();
			SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
			String[] retTime = {formatter.format(time)};
			
			tempSched.setScheduledTime(retTime);
			btn_next.setEnabled(true);
		}
	}
}



class ConfigSched_EditAlert
extends JPanel
{
	private JList lst_val;
	private JComboBox cmb_type;
	private ScheduleData tempSched;
	private JButton btn_next;
	
	
	public ConfigSched_EditAlert(ScheduleData tempSched, JButton btn_next, String method)
	{
		this.tempSched = tempSched;
		this.btn_next = btn_next;
		
		//System.out.println(tempSched.toXMLString());
		
		// Create components
		JLabel lbl_desc = new JLabel("Set the type of alert");
		JLabel lbl_type = new JLabel("Type:",JLabel.RIGHT);
		JSeparator sep  = new JSeparator();
		cmb_type        = new JComboBox();
		
		
		// Populate the combo box w/ the YearTypes
		ScheduleTypes.AlertTypes[] types = ScheduleTypes.AllAlertTypes();
		for (int i=0; i < types.length; i++)
			cmb_type.insertItemAt(types[i].toString(),i);
		
		// Attach listeners
		cmb_type.addActionListener(new AlertTypeListener());
		
		// If the ScheduleData item's YearType is set, make that our selected item in the combo box
		// This should trigger the list's listener which will populate it
		if (tempSched.getAlertType() != null)
			cmb_type.setSelectedItem( tempSched.getAlertType().toString() );
		else
			cmb_type.setSelectedIndex(0);
		
		
		btn_next.setEnabled(true);
		
		
		if (method.equals("add"))
		{
			lbl_desc.setText("Add the type of time");
		}
		else if (method.equals("del"))
		{
			lbl_desc.setText("View the type of time");
			cmb_type.setEnabled(false);
		}
		
		
		// Set layout
		GridBagConstraints gbc = new GridBagConstraints();;
		GridBagLayout gbl = new GridBagLayout();
		setLayout(gbl);
		gbc = new GridBagConstraints(0,0,4,1,1,0,GridBagConstraints.NORTH,GridBagConstraints.HORIZONTAL,new Insets(20,6,2,6),8,8);
		gbl.setConstraints(lbl_desc, gbc);
		gbc = new GridBagConstraints(0,1,4,1,1,0,GridBagConstraints.NORTH,GridBagConstraints.HORIZONTAL,new Insets(2,6,2,6),8,8);
		gbl.setConstraints(sep, gbc);
		gbc = new GridBagConstraints(0,2,1,1,0,1,GridBagConstraints.NORTH,GridBagConstraints.NONE,new Insets(2,6,2,6),8,8);
		gbl.setConstraints(lbl_type, gbc);
		gbc = new GridBagConstraints(1,2,1,1,0,1,GridBagConstraints.NORTH,GridBagConstraints.HORIZONTAL,new Insets(2,6,2,6),8,8);
		gbl.setConstraints(cmb_type, gbc);

		
		// Add components to the panel
		add(lbl_desc);
		add(lbl_type);
		add(sep);
		add(cmb_type);
	}
	
	
	class AlertTypeListener
	implements ActionListener
	{
		public AlertTypeListener()
		{
		}
		
		public void actionPerformed(ActionEvent e)
		{
			ScheduleTypes.AlertTypes at = ScheduleTypes.AlertToType( (String)cmb_type.getSelectedItem() );
			tempSched.setAlertType( at );
			
			btn_next.setEnabled(true);
		}
	}
	
}



class ConfigSched_EditStart
extends JPanel
{
	private JSpinner spn_val;
	private JCheckBox chk_allow;
	private ScheduleData tempSched;
	private JButton btn_next;
	
	
	public ConfigSched_EditStart(ScheduleData tempSched, JButton btn_next, String method)
	{
		this.tempSched = tempSched;
		this.btn_next = btn_next;
		
		//System.out.println(tempSched.toXMLString());
		
		// Create components
		JLabel lbl_desc = new JLabel("Set the schedules start date");
		JLabel lbl_val  = new JLabel("Start:",JLabel.RIGHT);
		JSeparator sep  = new JSeparator();
		chk_allow       = new JCheckBox("Modify?");
		spn_val         = new JSpinner();
		
		
		// Setup spinner
		Date today = new Date();
		SpinnerDateModel spn_mdl = new SpinnerDateModel(today,null,null,Calendar.MONTH);
		spn_val.setModel(spn_mdl);
		JSpinner.DateEditor spn_edt = new JSpinner.DateEditor(spn_val,"MM/dd/yyyy HH:mm");

		
		// Attach listeners
		chk_allow.addChangeListener(new AllowListener());
		spn_val.addChangeListener(new TimeValueListener());
		
		// Disable by default
		chk_allow.setSelected(false);
		spn_val.setEnabled(false);
		
		
		// set time's value
		String startDate = tempSched.getStartDate();
		if (startDate != null && !startDate.equals(""))
		{
			// This will set the date for the spinners model.  Only the hour:minutes matter,
			// but we pass in an entiere date
			try {
				Date valueDate = new SimpleDateFormat("MM/dd/yyyy HH:mm").parse( startDate );
				spn_val.setValue(valueDate);
			} catch (Exception ex) {
				System.err.println(ex.getMessage());
			}
		}
		else
		{
			Date now = new Date();
			spn_val.setValue(now);
			
			SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy HH:mm"); 
			tempSched.setStartDate(formatter.format(now));
		}
		
		
		if (method.equals("add"))
		{
			lbl_desc.setText("Add the schedules start date");
		}
		else if (method.equals("del"))
		{
			lbl_desc.setText("View the schedules start date");
			chk_allow.setEnabled(false);
			spn_val.setEnabled(false);
		}

		
		
		// Set layout
		GridBagConstraints gbc = new GridBagConstraints();;
		GridBagLayout gbl = new GridBagLayout();
		setLayout(gbl);
		gbc = new GridBagConstraints(0,0,4,1,1,0,GridBagConstraints.PAGE_START,GridBagConstraints.HORIZONTAL,new Insets(20,6,2,6),8,8);
		gbl.setConstraints(lbl_desc, gbc);
		gbc = new GridBagConstraints(0,1,4,1,1,0,GridBagConstraints.PAGE_START,GridBagConstraints.HORIZONTAL,new Insets(2,6,2,6),8,8);
		gbl.setConstraints(sep, gbc);
		gbc = new GridBagConstraints(0,2,2,1,0,1,GridBagConstraints.PAGE_START,GridBagConstraints.HORIZONTAL,new Insets(2,6,2,6),8,8);
		gbl.setConstraints(chk_allow, gbc);
		gbc = new GridBagConstraints(2,2,1,1,0,1,GridBagConstraints.PAGE_START,GridBagConstraints.HORIZONTAL,new Insets(2,6,2,6),8,8);
		gbl.setConstraints(lbl_val, gbc);
		gbc = new GridBagConstraints(3,2,1,1,1,1,GridBagConstraints.PAGE_START,GridBagConstraints.HORIZONTAL,new Insets(2,6,2,6),8,8);
		gbl.setConstraints(spn_val, gbc);
		
		// Add components to the panel
		add(lbl_desc);
		add(chk_allow);
		add(lbl_val);
		add(sep);
		add(spn_val);
	}
	
	
	class AllowListener
	implements ChangeListener
	{
		public AllowListener()
		{
		}
		
		public void stateChanged(ChangeEvent e)
		{
			if (chk_allow.isSelected())
				spn_val.setEnabled(true);
			else
				spn_val.setEnabled(false);
		}
	}
	
	
	class TimeValueListener
	implements ChangeListener
	{
		public TimeValueListener()
		{
		}
		
		public void stateChanged(ChangeEvent e)
		{
			Date time = (Date)spn_val.getValue();
			SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy HH:mm"); 
			
			tempSched.setStartDate(formatter.format(time));
		}
	}
}
